var app = getApp();
var Moment = require("../../utils/moment.js");
var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear();
var DATE_MONTH = new Date().getMonth() + 1;
var DATE_DAY = new Date().getDate();

Page({
  data: {
    homeArr: [],
    homeList: [],
    homeLength: "",
    pageCurrent: 1,
    pageNum: "", //总页数
    loading: false, //"上拉加载中"
    loadingComplete: false, //"加载完成"
    checkInDate: "",
    checkOutDate: "",
    imgList: "",
    houseList: "",
    imgUrl: app.globalData.imgUrl
  },
  onLoad: function(options) {
    //设缓存缓存起来的日期
    wx.setStorage({
      key: 'ROOM_SOURCE_DATE',
      data: {
        checkInDate: Moment(new Date()).format('YYYY-MM-DD'),
        checkOutDate: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD')
      }
    });
    if (options.url) {
      var path = options.url;
      if (options.id) {
        path += '?id=' + options.id;
      }
      wx.navigateTo({
        url: path
      })
    }
  },
  onShow: function() {
    var that = this;
    //获取缓存起来的日期
    let getDate = wx.getStorageSync("ROOM_SOURCE_DATE");
    this.setData({
      checkInDate: getDate.checkInDate,
      checkOutDate: getDate.checkOutDate,
      homeList: [],
      pageCurrent: 1
    });
    that.getImg();
    that.homeStayList();
  },
  onShareAppMessage() {
    return {
      title: '在海花岛这样订房也不错，快看快约！',
      path: '/pages/index/index'
    }
  },
  getImg: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/index/bannerList",
      data: {
        position: "slider"
      },
      header: {
        'content-type': 'application/json'
      },
      method: "GET",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var imgData = res.data.data;
          that.setData({
            imgList: imgData
          })
        }
      },

    });
  },
  homeStayList: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiHost + "/api/index/houseList",
      data: {
        page: that.data.pageCurrent,
        pagesize: 6,
        flag: "hot"
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'token=' + wx.getStorageSync('token')
      },
      method: "POST",
      dataType: "json",
      success: function(res) {
        if (res.statusCode === 200) {
          var listLength = res.data.data.data.length;
          var listPageNum = res.data.data.last_page;
          for (var i = 0; i < res.data.data.data.length; i++) {
            that.data.homeList.push(res.data.data.data[i]);
          }
          var houseData = that.data.homeList;
          that.setData({
            homeArr: houseData,
            pageNum: listPageNum,
            homeLength: listLength
          })
        }
      },

    });
  },
  cartOperate: function(e) {
    var that = this;
    var proId = e.currentTarget.dataset.id;
    var proIndex = e.currentTarget.dataset.key;
    //改变心愿单选择的状态
    if (that.data.homeArr[proIndex].cart_exists == 1) {
      that.data.homeArr[proIndex].cart_exists = 0;
      app.cartDelete(proId, 1);
    } else if (that.data.homeArr[proIndex].cart_exists == 0) {
      that.data.homeArr[proIndex].cart_exists = 1;
      app.cartAdd(proId, 1);
    }
    that.setData({
      homeArr: that.data.homeArr
    })
  },
  onReachBottom: function() {
    let that = this;
    if (that.data.pageCurrent < that.data.pageNum) {
      that.setData({
        loading: true,
        loadingComplete: false,
        pageCurrent: that.data.pageCurrent + 1
      })
      that.homeStayList();
    } else if (that.data.pageCurrent >= that.data.pageNum) {
      that.setData({
        loading: false,
        loadingComplete: true
      })
    }
  }
})